package com.zy.imagineCommunity.controller;

import com.alibaba.fastjson.JSONObject;
import com.zy.imagineCommunity.entity.*;
import com.zy.imagineCommunity.service.CommentService;
import com.zy.imagineCommunity.service.UserSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class CommentController {

    @Autowired
    CommentService commentService;

    @Autowired
    UserSerivce userSerivce;

    @ResponseBody
    @GetMapping("/getCommentList")
    public JSONObject commentList(int blogId, int pageNo) {
        //查询这一页的信息
        Page<CommentText> page = commentService.queryBlogByPraiseAndPageNoAndPageSize(blogId, pageNo, 10);

        for (CommentText comment : page.getItems()) {
            System.out.println(comment);
            for (Object reply : comment.getItems()) {
                System.out.println(reply);
            }
        }

        JSONObject jsonObject = new JSONObject();

        jsonObject.put("page", page);

        return jsonObject;
    }

    /**
     * 增加评论
     *
     * @param userId
     * @param blogId
     * @param content
     * @param type
     * @return
     */
    @ResponseBody
    @GetMapping("/addComment")
    public JSONObject addComment(int userId, int blogId, String content, int type) {

        JSONObject jsonObject = new JSONObject();

        User user = userSerivce.queryUserByUserId(userId);

        String stateString = user.getLanguageState();

        String[] stateNumber = stateString.split("_");

        for (int i = 0; i < stateNumber.length; i++) {

            int state = Integer.parseInt(stateNumber[i]);

            if (state == 1) {

                jsonObject.put("comment", "no");

                jsonObject.put("msg", "你已经被超级管理员禁言,无法发表评论!");

                return jsonObject;
            }

            if (state == type) {

                jsonObject.put("comment", "no");

                jsonObject.put("msg", "你已经被该版主禁言,无法发表评论!");

                return jsonObject;
            }

        }

        Date date = new Date();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        Comment comment = new Comment(null, userId, blogId, content, simpleDateFormat.format(date), type);

        commentService.addComment(comment);

        //返回产生的评论id
        jsonObject.put("commentId", comment.getId());

        jsonObject.put("comment", "yes");

        jsonObject.put("msg", "评论成功!");

        return jsonObject;
    }

    /**
     * 增加回复
     *
     * @param userId
     * @param commentId
     * @param content
     * @param type
     * @return
     */
    @ResponseBody
    @GetMapping("/addReply")
    public JSONObject addReply(int userId, int commentId, String content, int type) {

        JSONObject jsonObject = new JSONObject();

        User user = userSerivce.queryUserByUserId(userId);

        String stateString = user.getLanguageState();

        String[] stateNumber = stateString.split("_");

        for (int i = 0; i < stateNumber.length; i++) {

            int state = Integer.parseInt(stateNumber[i]);

            if (state == 1) {

                jsonObject.put("reply", "no");

                jsonObject.put("msg", "你已经被超级管理员禁言,无法回复!");

                return jsonObject;
            }

            if (state == type) {

                jsonObject.put("reply", "no");

                jsonObject.put("msg", "你已经被该版主禁言,无法回复!");

                return jsonObject;
            }

        }

        Date date = new Date();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        commentService.addReply(new Reply(null, userId, commentId, content, simpleDateFormat.format(date), type));

        jsonObject.put("reply", "yes");

        jsonObject.put("msg", "回复成功!");

        return jsonObject;
    }

    @GetMapping("/toPersonalCommentsList")
    private String toPersonalCommentsList() {
        return "/PersonalCommentList";
    }

    @ResponseBody
    @GetMapping("/getPersonalCommentsList")
    public JSONObject getPersonalCommentsList(int userId, int pageNo) {
        //查询这一页的信息
        Page<CommentTextTwo> page = commentService.queryPersonalCommentsByUserIdAndPageNoAndPageSize(userId, pageNo, 10);

        JSONObject jsonObject = new JSONObject();

        jsonObject.put("page", page);

        return jsonObject;
    }

    @ResponseBody
    @GetMapping("/deleteComment")
    public void deleteComment(int commentId) {
        //删除评论以及下面的回复
        commentService.deleteCommentByCommentId(commentId);
        commentService.deleteReplyByCommentId(commentId);
    }

    @ResponseBody
    @GetMapping("/deleteReply")
    public void deleteReply(int id) {
        //删除回复
        commentService.deleteReplyById(id);
    }

    @GetMapping("/toPersonalReplyList")
    private String toPersonalReplyList() {
        return "/PersonalReplyList";
    }

    @ResponseBody
    @GetMapping("/getPersonalReplyList")
    public JSONObject getPersonalReplyList(int userId, int pageNo) {
        //查询这一页的信息
        Page<ReplyTextTwo> page = commentService.queryPersonalReplyByUserIdAndPageNoAndPageSize(userId, pageNo, 10);

        JSONObject jsonObject = new JSONObject();

        jsonObject.put("page", page);

        return jsonObject;
    }

}
